package com.may._6;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) throws Exception {
        SynchronousQueue queue = new SynchronousQueue();
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+"\t A put 1");
                queue.put("1");
                System.out.println(Thread.currentThread().getName()+"\t A put 2");
                queue.put("2");
                System.out.println(Thread.currentThread().getName()+"\t A put 3");
                queue.put("3");
            }catch (Exception e){
                e.printStackTrace();
            };
        },"A").start();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+"\t B take");
                queue.take();
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+"\t B take");
                queue.take();
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+"\t B take");
                queue.take();
            }catch (Exception e){
                e.printStackTrace();
            };
        },"B").start();
    }
}
